package se.perp.c4.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import se.perp.c4.model.Actor;
import se.perp.c4.model.Actor.EActor;
import se.perp.c4.model.ActorCollection;
import static se.perp.c4.model.Actor.EActor.*;

public class Portraits {

    private static Map<EActor, BufferedImage> images = new HashMap<EActor, BufferedImage>();
    private static Map<EActor, Icon> icons = new HashMap<EActor, Icon>();
    private static BufferedImage defaultPortrait = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);


    static {
        Graphics g = defaultPortrait.getGraphics();
        g.setColor(Color.ORANGE);
        g.fillRect(0, 0, 100, 100);
        g.setColor(Color.BLACK);
        g.drawString("No image", 10, 40);

        images.put(FRODO, load("/images/frodo_1s.jpg"));
        images.put(SAM, load("/images/sam_1s.jpg"));
        images.put(MERRY, load("/images/merry_1s.jpg"));
        images.put(PIPPIN, load("/images/pippin_1s.jpg"));
        images.put(ARAGORN, load("/images/aragorn_1s.jpg"));
        images.put(GANDALF, load("/images/gandalf_1s.jpg"));
        images.put(LEGOLAS, load("/images/legolas_1s.jpg"));
        images.put(GIMLI, load("/images/gimli_1s.jpg"));
        images.put(BOROMIR, load("/images/boromir_1s.jpg"));

        images.put(CAVETROLL, load("/images/cavetroll_1s.jpg"));
        images.put(WARG, load("/images/warg_1s.jpg"));
        images.put(WITCHKING, load("/images/witchking_1s.jpg"));
        images.put(SHELOB, load("/images/shelob_1s.jpg"));
        images.put(SARUMAN, load("/images/saruman_1s.jpg"));
        images.put(ORCS, load("/images/orcs_1s.jpg"));
        images.put(BALROG, load("/images/balrog_1s.jpg"));
        images.put(B_RIDERS, load("/images/riders_1s.jpg"));
        images.put(F_NAZGUL, load("/images/nazgul_1s.jpg"));

        for (EActor ea : images.keySet()) {
            Image img = get(ea).getScaledInstance(50, 50, Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(img);
            icons.put(ea, icon);
        }
    }

    private static BufferedImage load(String path) {
        try {
            BufferedImage img = ImageIO.read(Portraits.class.getResourceAsStream(path));
            Graphics g = img.getGraphics();
            g.setColor(Color.BLACK);
            g.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1);
            return img;
        } catch (Exception e) {
            Logger.getLogger(Portraits.class.getName()).log(
                    Level.SEVERE, "Could not read portrait image " + path);
            return defaultPortrait;
        }
    }

    public static BufferedImage get(EActor who) {
        return images.get(who);
    }

    public static Icon getIcon(EActor who) {
        return icons.get(who);
    }

    public static BufferedImage getNumberedPortrait(
            Actor.EActor which, int number) {
        BufferedImage portrait = get(which);

        BufferedImage numberedPortrait = new BufferedImage(portrait.getWidth(null),
                portrait.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) numberedPortrait.getGraphics();
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.drawImage(portrait, 0, 0, null);

        g.setFont(FontSet.PORTRAIT_ATK_VALUE);
        String str = String.valueOf(number);
        int strw = g.getFontMetrics().stringWidth(str);

        int x = numberedPortrait.getWidth(null) - strw - 3;
        int y = numberedPortrait.getHeight(null) - 3;
        g.setColor(Color.BLACK);
        g.drawString(str, x + 1, y + 1);
        g.setColor(Color.WHITE);
        g.drawString(str, x, y);

        return numberedPortrait;
    }

    public static BufferedImage getLabeledNumberedPortrait(
            Actor.EActor which, String label, int number) {
        BufferedImage numbered = getNumberedPortrait(which, number);

        BufferedImage np = new BufferedImage(numbered.getWidth(null),
                numbered.getHeight(null),
                BufferedImage.TYPE_INT_RGB);

        Graphics2D g = (Graphics2D) np.getGraphics();
        g.drawImage(numbered, 0, 0, null);
        g.setFont(FontSet.PORTRAIT_LABEL);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        int strw = g.getFontMetrics().stringWidth(label);
        int strh = g.getFontMetrics().getHeight();
        int x = (np.getWidth() - strw) / 2;
        int y = strh;
        g.setColor(Color.BLACK);
        g.drawString(label, x + 1, y + 1);
        g.setColor(Color.WHITE);
        g.drawString(label, x, y);

        return np;
    }

    public static BufferedImage getLabeledNumberedNamedPortrait(
            Actor actor, String label, int number) {
        BufferedImage img = getLabeledNumberedPortrait(actor.who, label, number);
        Graphics2D g = (Graphics2D) img.getGraphics();

        g.setFont(FontSet.PORTRAIT_LABEL);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        int y = img.getHeight() -5;
        int x = 3;

        g.setColor(Color.BLACK);
        g.drawString(actor.portraitName, x+1, y + 1);
        g.setColor(Color.WHITE);
        g.drawString(actor.portraitName, x, y);

        return img;
    }

    static class TestFrame extends JFrame {

        class P extends JPanel {

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(b, 20, 20, null);
            }
        }
        private BufferedImage b;

        public TestFrame(BufferedImage b) {
            super("Foo");
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            this.b = b;
            setSize(120, 120);
            add(new P());
            setVisible(true);
        }
    }

    public static void main(String[] args) {
        ActorCollection actorColl = new ActorCollection();
        BufferedImage b = Portraits.getLabeledNumberedNamedPortrait(actorColl.get(SARUMAN), "Defender", 5);

        new TestFrame(b);
    }
}
